use std::process::Command;
use std::assert;

struct BuildInfo {
    outdir: String,
}

fn main() {
    let outdir = std::env::var("OUT_DIR").unwrap();
    let build_info = BuildInfo {
        outdir,
    };
    build_gresource(&build_info);
}

fn build_gresource(build_info: &BuildInfo) {
    let status = Command::new("glib-compile-resources")
                    .arg("--sourcedir=data/resources")
                    .arg(format!("--target={}/resources.c", build_info.outdir))
                    .arg("--generate-source")
                    .arg("--internal")
                    .arg("data/resources/resources.gresource.xml")
                    .status().unwrap();
    assert!(status.success());

    let output = Command::new("pkg-config")
                    .args(&["--cflags", "gtk+-3.0"])
                    .output().unwrap();
    assert!(output.status.success());
    let output = String::from_utf8(output.stdout).unwrap();
    let cflags_gtk = output.split_whitespace();

    let mut build = cc::Build::new();
    for each_flag in cflags_gtk {
        build.flag(each_flag);
    }
    build.file(format!("{}/resources.c", build_info.outdir))
        .compile("resources");
    println!("cargo:rustc-link-lib=resources");
    // Rebuild if resource changed
    println!("cargo:rerun-if-changed=data/resources/");
}
